<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\CaseCategory;
use App\Models\News;
use App\Models\NewsCategory;
use App\Models\ProductCategory;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    public function product(Request $request, ProductCategory $category)
    {
        // pid 不传时默认查全部，传了查对应的分类
        $pid = (int)$request->input('pid');

        return $category->select(['id', 'pid', 'name'])
            ->where('hide', 0)
            ->when($pid >= 0, function ($query) use ($pid) {
                return $query->where('pid', $pid);
            })
            ->latest('orderby')
            ->get();
    }

    public function cases(Request $request, CaseCategory $category)
    {
        $fields = $category->getFillable();
        return $category->select(['id', ...$fields])
            ->where('hide', 0)
            ->latest('orderby')
            ->get();
    }

    public function news(Request $request, NewsCategory $category)
    {
        return $category->select('id', 'name')->where('hide', 0)
            ->latest('orderby')
            ->get();
    }
}
